/**
 * ProtoActor WASM 客户端测试脚本 (Bun)
 * 
 * 此脚本用于测试 ProtoActor WASM 服务器 API
 * 运行方式: bun run src/examples/bun-client-example.ts
 */

// 服务器 URL
const SERVER_URL = 'http://localhost:3000';

// 测试函数
async function runTests() {
  console.log('========================================');
  console.log('  ProtoActor WASM 客户端测试开始');
  console.log('========================================');
  
  try {
    // 测试创建 Actor
    console.log('\n-- 测试创建 Actor');
    const actor1 = await createActor('testActor1');
    const actor2 = await createActor('testActor2');
    
    // 测试获取所有 Actor
    console.log('\n-- 测试获取所有 Actor');
    await listActors();
    
    // 测试发送消息
    console.log('\n-- 测试发送消息');
    await sendMessage('testActor1', { type: 'Greeting', content: '你好，世界！' });
    await sendMessage('testActor2', { type: 'Command', action: 'doSomething', data: { value: 42 } });
    
    // 测试对不存在的 Actor 发送消息（应该会自动创建）
    console.log('\n-- 测试对不存在的 Actor 发送消息');
    await sendMessage('newActor', { type: 'NewActorTest', content: '自动创建 Actor' });
    
    // 测试获取所有 Actor
    console.log('\n-- 再次测试获取所有 Actor');
    await listActors();
    
    // 测试停止 Actor
    console.log('\n-- 测试停止 Actor');
    await stopActor('testActor1');
    
    // 最终获取剩余的 Actor
    console.log('\n-- 最终获取所有 Actor');
    await listActors();
    
    console.log('\n========================================');
    console.log('  测试完成');
    console.log('========================================');
  } catch (error) {
    console.error('测试失败:', error);
  }
}

// 创建 Actor
async function createActor(name: string) {
  console.log(`创建 Actor: ${name}`);
  
  try {
    const response = await fetch(`${SERVER_URL}/api/create-actor`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ name })
    });
    
    const result = await response.json();
    console.log('创建结果:', result);
    return result;
  } catch (error) {
    console.error(`创建 Actor 失败: ${error}`);
    throw error;
  }
}

// 获取所有 Actor
async function listActors() {
  console.log('获取所有 Actor');
  
  try {
    const response = await fetch(`${SERVER_URL}/api/list-actors`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({})
    });
    
    const result = await response.json();
    console.log('当前 Actor 列表:', result.actors);
    return result;
  } catch (error) {
    console.error(`获取 Actor 列表失败: ${error}`);
    throw error;
  }
}

// 发送消息
async function sendMessage(actorName: string, message: any) {
  console.log(`发送消息到 Actor ${actorName}:`, message);
  
  try {
    const response = await fetch(`${SERVER_URL}/api/send-message`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ actorName, message })
    });
    
    const result = await response.json();
    console.log('消息发送结果:', result);
    return result;
  } catch (error) {
    console.error(`发送消息失败: ${error}`);
    throw error;
  }
}

// 停止 Actor
async function stopActor(actorName: string) {
  console.log(`停止 Actor: ${actorName}`);
  
  try {
    const response = await fetch(`${SERVER_URL}/api/stop-actor`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ actorName })
    });
    
    const result = await response.json();
    console.log('停止结果:', result);
    return result;
  } catch (error) {
    console.error(`停止 Actor 失败: ${error}`);
    throw error;
  }
}

// 执行测试
runTests().catch(console.error); 